<template>
  <div>
    <el-card class="box-card">
      <el-form :inline="true" :model="form" class="demo-form-inline">
        <el-form-item label="一级分类">
          <el-select
            :disabled="disabled"
            v-model="form.categoryID1"
            placeholder="请选择"
            @change="handle1"
          >
            <el-option
              v-for="(item, index) in list1"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类" >
          <el-select
            :disabled="disabled"
            v-model="form.categoryID2"
            placeholder="请选择"
            @change="handle2"
          >
            <el-option
              v-for="(item, index) in list2"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="三级分类" >
          <el-select
            :disabled="disabled"
            v-model="form.categoryID3"
            placeholder="请选择"
            @change="handle3"
          >
            <el-option
              v-for="(item, index) in list3"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "Category",
  props:{
    disabled:{
      type:Boolean,
      default:false
    }
  },
  data() {
    return {
      list1: [],
      list2: [],
      list3: [],
      form: {
        categoryID1: "",
        categoryID2: "",
        categoryID3: "",
      },
    };
  },
  mounted() {
    this.getCategory1();
  },
  methods: {
    async getCategory1() {
      const { data } = await this.$API.category.getCategory1();
      this.list1 = data;
    },
    async getCategory2(id) {
      const { data } = await this.$API.category.getCategory2(id);
      this.list2 = data;
    },
    async getCategory3(id) {
      const { data } = await this.$API.category.getCategory3(id);
      this.list3 = data;
    },
    async handle1() {
      this.form.categoryID2 = "";
      this.form.categoryID3 = "";
      this.list2 = [];
      this.list3 = [];
      this.getCategory2(this.form.categoryID1);
      this.$emit('categoryChange',this.form)
    },
    async handle2() {
      this.form.categoryID3 = "";
      this.list3 = [];
      this.getCategory3(this.form.categoryID2);
      this.$emit('categoryChange',this.form)

    },
    async handle3() {
      this.$emit('categoryChange',this.form)
    },
  },
};
</script>

<style scoped>
</style>